package com.t2t.top.util;

import com.taobao.diamond.manager.DiamondManager;
import com.taobao.diamond.manager.ManagerListener;
import com.taobao.diamond.manager.impl.DefaultDiamondManager;
import org.apache.commons.lang.StringUtils;

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.UnsupportedEncodingException;
import java.util.Date;
import java.util.Properties;
import java.util.concurrent.Executor;
import java.util.concurrent.locks.ReentrantLock;

/**
 * Created by yangpengfei on 2016/2/5.
 */
public class ConfigFactory {
    public static final String GROUP_KEY_DOCKER_WEB = "DOCKER-WEB";
    public static final String DATA_ID_COMMONS = "COMMONS";

    //单例对象
    private static ConfigFactory factory;

    //公共配置
    private Properties localConfig = new Properties();

    public synchronized static ConfigFactory getFactory() {
        if(factory == null){
            factory = new ConfigFactory();
        }
        return factory;
    }

    private ConfigFactory() {
        DiamondManager manager = new DefaultDiamondManager(GROUP_KEY_DOCKER_WEB, DATA_ID_COMMONS, new ManagerListener() {
            public Executor getExecutor() {return null;}
            public void receiveConfigInfo(String configInfo) {
                System.out.println("changed config: " + configInfo);
                rebuidConfig(configInfo);
            }
        }, "121.42.161.149");
        manager.getDiamondConfigure().setPort(8003);//设置diamond-server服务的端口
        rebuidConfig(manager.getAvailableConfigureInfomation(5000));
    }

    /**
     * 重新构造缓存配置
     */
    public void rebuidConfig(String configInfo) {
        if (StringUtils.isBlank(configInfo)) {
            return;
        }

        System.out.println("start config: " + configInfo);

        ReentrantLock lock = new ReentrantLock();
        InputStream in = null;
        try {
            in = new ByteArrayInputStream(configInfo.getBytes("UTF-8"));
        } catch (UnsupportedEncodingException e) {
            System.out.println(e);
        }
        try {
            lock.lock();
            Properties temp = new Properties();
            temp.load(in);
            setLocalConfig(temp);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                if (in != null)
                    in.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
            lock.unlock();
        }
    }

    public Properties getLocalConfig() {
        return localConfig;
    }

    public void setLocalConfig(Properties localConfig) {
        this.localConfig = localConfig;
    }

    public static String getProperty(String name) {
        return ConfigFactory.getFactory().getLocalConfig().getProperty(name);
    }

    public static void main(String[] args) {
        long s = new Date().getTime();
        System.out.println(ConfigFactory.getProperty("jdbc.url"));
        long e = new Date().getTime();
        System.out.println((e - s) + "毫秒");
    }
}
